package com.esiran.payadmin.entity;

import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;

import java.util.HashMap;
import java.util.Map;

public class FormInputField extends FormField{
    private String placeholder;
    private boolean autocomplete;
    private String rules;
    public static class Builder {
        private String placeholder;
        private boolean autocomplete;
        private Map<String,Map<String,String>> rule;
        public Builder(){
            rule = new HashMap<>();
        }
        public Builder setPlaceholder(String placeholder) {
            this.placeholder = placeholder;
            return this;
        }
        public Builder setAutocomplete(boolean autocomplete) {
            this.autocomplete = autocomplete;
            return this;
        }
        public Builder addRule(String key,String val){
            addRule(key,val,null);
            return this;
        }
        public Builder addRule(String key,String val,String errMsg){
            Map<String,String> ruleVal = new HashMap<>();
            ruleVal.put("ops",val);
            if (errMsg!=null) ruleVal.put("errMsg",errMsg);
            rule.put(key,ruleVal);
            return this;
        }
        public FormInputField create(String key,String name){
            return new FormInputField(key,name,this);
        }
    }
    private FormInputField(String key, String name,Builder builder) {
        super(key, name,"text");
        placeholder = builder.placeholder;
        autocomplete = builder.autocomplete;
        StringBuilder ruleString = new StringBuilder();
        for(String rule: builder.rule.keySet()){
            Map<String,String> ruleData = builder.rule.get(rule);
                String ruleOps = ruleData.get("ops");
            ruleString.append(rule)
                    .append("=\"")
                    .append(ruleOps)
                    .append("\"");
            String errMsg = ruleData.get("errMsg");
            if (errMsg != null){
                ruleString.append(" data-parsley-")
                        .append(rule).append("-message=\"")
                        .append(errMsg).append("\"");
            }
            ruleString.append("\n");
        }
        rules = ruleString.toString();
    }

    public String getPlaceholder() {
        return placeholder;
    }

    public boolean isAutocomplete() {
        return autocomplete;
    }

    public String getRules() {
        return rules;
    }
}
